package com.codrim.cs.data.server.configuration;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.codrim.common.rpc.dubbo.CodrimDubboAnnotationBean;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Configuration for Dubbo Created by Liang.zhuge on 13/02/2017.
 */
@Configuration
public class DubboConfiguration {

    private @Value("${dubbo.application.name}") String applicationName;
    private @Value("${dubbo.registry.address}") String registryAddress;
    private @Value("${dubbo.registry.protocol}") String registryProtocol;
    private @Value("${dubbo.registry.port}") Integer registryPort;
    private @Value("${dubbo.registry.group}") String registryGroup;
    private @Value("${dubbo.provider.timeout}") Integer timeout;
    private @Value("${dubbo.provider.retries}") Integer retries;
    private @Value("${dubbo.provider.delay}") Integer delay;
    private @Value("${dubbo.protocol.host}") String protocolHost;

    @Bean
    public static CodrimDubboAnnotationBean annotationBean(
            @Value("${dubbo.annotation.package}") String packName) {
        final CodrimDubboAnnotationBean bean = new CodrimDubboAnnotationBean();
        bean.setPackage(packName);
        return bean;
    }

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(this.applicationName);
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        final RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(this.registryAddress);
        registryConfig.setProtocol(this.registryProtocol);
        registryConfig.setGroup(this.registryGroup);
        return registryConfig;
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        final ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(this.registryPort);
        protocolConfig.setThreads(200);
        if (StringUtils.isNotBlank(protocolHost)) {
            protocolConfig.setHost(protocolHost);
        }
        return protocolConfig;
    }

    @Bean(name = "defaultProvider")
    public ProviderConfig providerConfig(ApplicationConfig applicationConfig,
            RegistryConfig registryConfig, ProtocolConfig protocolConfig) {
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setTimeout(timeout);
        providerConfig.setRetries(retries);
        providerConfig.setDelay(delay);
        providerConfig.setApplication(applicationConfig);
        providerConfig.setRegistry(registryConfig);
        providerConfig.setProtocol(protocolConfig);
        return providerConfig;
    }

}
